/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package talvan;

import java.util.ArrayList;

/**
 * klassi sum skal senda sql querys til dátagrunnin
 * @author DJH
 */
public class SQLCallerParticipantsList {
    
    private DbHandler dbh;
    
    /**
     * constructor sum tekur ein dbhandler sum parametur
     * @param dbh 
     */
    public SQLCallerParticipantsList(DbHandler dbh)
    {
        this.dbh = dbh;
    }
    
    
    /**
     * metoda sum hentar employes sum eri inkalla til hendanfundin
     * @param int devision
     * @return lista við starvsfólkum sum eri innkalla til hendan fundin
     */
    public ArrayList<Employe> getEmployesForMeeting(int meetingID)
    {
        ArrayList<Employe> resultList = new ArrayList<Employe>();
        java.sql.ResultSet rs = null;
        try {
            if (dbh.getConnection() != null) {
                String sql = "USE TALVAN; ";
                sql += "SELECT Employe.firstName as fName, Employe.lastName as lName, Employe.id as id, Employe.devision_id as devision, Devision.name as dName "
                     + "FROM Employe, Participant, Devision "
                     + "WHERE Participant.employe_id = Employe.id AND Devision.id = Employe.devision_id AND Participant.meeting_id = " + meetingID + "; ";
                
                
                java.sql.Statement stmt = dbh.getConnection().createStatement();
                rs = stmt.executeQuery(sql);
                
                //loop sum creatar objects og addar í array ella Arraylist?
                while (rs.next()) {
                    Employe newEmploye = new Employe();
                    
                    newEmploye.setFirstName(rs.getString("fName"));
                    newEmploye.setLastName(rs.getString("lName"));
                    String idString = rs.getString("id");
                    newEmploye.setID(Integer.parseInt(idString));
                    String stringDevisionID = rs.getString("devision");
                    newEmploye.setDevisionID(Integer.parseInt(stringDevisionID));
                    newEmploye.setDevisionName(rs.getString("dName"));
                    resultList.add(newEmploye);
                    
                    
                }
                rs.close();
                rs = null;
                dbh.closeConnection();
            } else {
                throw new Exception("Error: No active Connection");
            }
        } catch (Exception e) {
            //throw new Exception("SQL statement í DataHandler (getMeetings metodan)");
        }
       
        return resultList;
    }
    
}
